package com.example.app2024.wechatr;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.app2024.R;

import java.util.ArrayList;
import java.util.List;

public class chatActivity extends AppCompatActivity implements View.OnClickListener
{
      private ViewPager viewPager;  //自带滑动效果
      private List<Fragment> fragmentList;
      private RadioButton r1, r2, r3, r4;

      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            Log.i("TAG", "aaaaaaaa++++++++++++++++++++++");
            super.onCreate(savedInstanceState);
            EdgeToEdge.enable(this);
            setContentView(R.layout.activity_chat);
            ViewCompat.setOnApplyWindowInsetsListener(
                    findViewById(R.id.main), (v, insets) -> {
                          Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                          v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
                          return insets;
                    });

            r1 = findViewById(R.id.rb_wx);
            r2 = findViewById(R.id.rb_txl);
            r3 = findViewById(R.id.rb_fx);
            r4 = findViewById(R.id.rb_wo);

            r1.setOnClickListener(this);
            r2.setOnClickListener(this);
            r3.setOnClickListener(this);
            r4.setOnClickListener(this);

            viewPager = findViewById(R.id.vp);
            fragmentList = new ArrayList<Fragment>();
            fragmentList.add(new fragment1());
            fragmentList.add(new fragment2());
            fragmentList.add(new fragment3());
            fragmentList.add(new fragment4());

            FragmentManager fragmentManager = this.getSupportFragmentManager();
            MyPagerAdapter adapter = new MyPagerAdapter(fragmentManager);

            viewPager.setAdapter(adapter);
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
            {
                  @Override
                  public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
                  {
                        if (position == 0)
                        {
                              r1.setChecked(true);
                              r2.setChecked(false);
                              r3.setChecked(false);
                              r4.setChecked(false);
                        }
                        else if (position == 1)
                        {
                              r1.setChecked(false);
                              r2.setChecked(true);
                              r3.setChecked(false);
                              r4.setChecked(false);
                        }
                        else if (position == 2)
                        {
                              r1.setChecked(false);
                              r2.setChecked(false);
                              r3.setChecked(true);
                              r4.setChecked(false);
                        }
                        else if (position == 3)
                        {
                              r1.setChecked(false);
                              r2.setChecked(false);
                              r3.setChecked(false);
                              r4.setChecked(true);
                        }

                  }

                  @Override
                  public void onPageSelected(int position)
                  {

                  }

                  @Override
                  public void onPageScrollStateChanged(int state)
                  {

                  }
            });

      }

      @Override
      public void onClick(View v)
      {
            int id = v.getId();
            if (id == R.id.rb_wx)
            {
                  viewPager.setCurrentItem(0);
            }
            else if (id == R.id.rb_txl)
            {
                  viewPager.setCurrentItem(1);
            }
            else if (id == R.id.rb_fx)
            {
                  viewPager.setCurrentItem(2);
            }
            else if (id == R.id.rb_wo)
            {
                  viewPager.setCurrentItem(3);
            }
      }

      private class MyPagerAdapter extends FragmentPagerAdapter
      {
            public MyPagerAdapter(@NonNull FragmentManager fm)
            {
                  super(fm);
            }

            @NonNull
            @Override
            public Fragment getItem(int position)
            {
                  return fragmentList.get(position);
            }

            @Override
            public int getCount()
            {
                  return fragmentList.size();
            }
      }


}